package com.yanqu.road.server.gameplayer.module.activity.luckydraw;

import com.yanqu.road.entity.activity.luckydraw.LuckyDrawConfig;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.activity.LuckyDrawProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.luckydraw.LuckyDrawMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.pb.LuckyDrawPb;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

public class LuckyDrawModule extends GeneralModule {

    public void refreshData() {
        syncConfig();
    }

    private void syncConfig() {
        LuckyDrawConfig config = LuckyDrawMgr.getLuckyDrawConfig();
        if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            LuckyDrawProto.LuckyDrawSyncConfigRespMsg.Builder respMsg = LuckyDrawPb.parseLuckyDrawSyncConfigRespMsg(config);
            player.sendPacket(ClientProtocol.U_LUCKY_DRAW_SYNC_CONFIG, respMsg);
        }
    }

    private LuckyDrawProto.ChannelLuckyDrawKeyMsg.Builder parseChannelLuckyDrawKeyMsg() {
        LuckyDrawConfig config = LuckyDrawMgr.getLuckyDrawConfig();
        int activityId = config == null ? 0 : config.getActivityInfo().getActivityId();
        long serverId = GameServer.getInstance().getServerId();
        int channelId = ServerListMgr.getChannelId(serverId);

        LuckyDrawProto.ChannelLuckyDrawKeyMsg.Builder builder = LuckyDrawProto.ChannelLuckyDrawKeyMsg.newBuilder();
        builder.setChannelId(channelId);
        builder.setServerId(serverId);
        builder.setUserId(getUserId());
        builder.setZeroTime(DateHelper.getTodayZeroTimeStamp());
        builder.setNickname(player.getUserInfo().getNickName());
        builder.setActivityId(activityId);
        return builder;
    }

    // -------------------------------

    /**
     * 抽奖
     */
    public int draw() {

        LuckyDrawConfig config = LuckyDrawMgr.getLuckyDrawConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if (!SystemOpenMgr.systemOpen(player, eSystemId.LuckyDrawActivity.getValue())) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        LuckyDrawProto.ChannelLuckyDrawKeyMsg.Builder keyMsg = parseChannelLuckyDrawKeyMsg();
        LuckyDrawProto.ChannelLuckyDrawDrawReqMsg.Builder builder = LuckyDrawProto.ChannelLuckyDrawDrawReqMsg.newBuilder();
        builder.setKey(keyMsg);
        player.sendChannelPacket(ChannelProtocol.N_LUCKY_DRAW_DRAW, builder);
        return 0;
    }

    /**
     * 添加瓦舍获胜次数
     */
    public int addWasheWinTimes() {

        LuckyDrawConfig config = LuckyDrawMgr.getLuckyDrawConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if (!SystemOpenMgr.systemOpen(player, eSystemId.LuckyDrawActivity.getValue())) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        LuckyDrawProto.ChannelLuckyDrawKeyMsg.Builder keyMsg = parseChannelLuckyDrawKeyMsg();
        LuckyDrawProto.ChannelLuckyDrawAddWasheWinTimes.Builder builder = LuckyDrawProto.ChannelLuckyDrawAddWasheWinTimes.newBuilder();
        builder.setKey(keyMsg);
        player.sendChannelPacket(ChannelProtocol.N_LUCKY_DRAW_ADD_WASHE_WIN_TIMES, builder);
        return 0;
    }

    /**
     * 获取抽奖公告
     */
    public int getDrawRecord() {

        LuckyDrawConfig config = LuckyDrawMgr.getLuckyDrawConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        LuckyDrawProto.ChannelLuckyDrawKeyMsg.Builder keyMsg = parseChannelLuckyDrawKeyMsg();
        LuckyDrawProto.ChannelLuckyDrawGetRecordReqMsg.Builder builder = LuckyDrawProto.ChannelLuckyDrawGetRecordReqMsg.newBuilder();
        builder.setKey(keyMsg);
        player.sendChannelPacket(ChannelProtocol.N_LUCKY_DRAW_GET_RECORD, builder);
        return 0;
    }

    /**
     * 获取玩家数据
     */
    public int getUserData() {
        LuckyDrawConfig config = LuckyDrawMgr.getLuckyDrawConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if (!SystemOpenMgr.systemOpen(player, eSystemId.LuckyDrawActivity.getValue())) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        LuckyDrawProto.ChannelLuckyDrawKeyMsg.Builder keyMsg = parseChannelLuckyDrawKeyMsg();
        LuckyDrawProto.ChannelLuckyDrawGetUserDataReqMsg.Builder builder = LuckyDrawProto.ChannelLuckyDrawGetUserDataReqMsg.newBuilder();
        builder.setKey(keyMsg);
        player.sendChannelPacket(ChannelProtocol.N_LUCKY_DRAW_GET_USER_DATA, builder);
        return 0;
    }

    // -------------------------------

    @Override
    public void afterLogin() {
        refreshData();
    }

    public LuckyDrawModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }
}
